# 字符串格式化shi'  
# %s  %d  %f 占位符
print("hello %s" % "world") # hello world
# %d  decimal 整数
print("hello %d" % 123) # hello 123
# %f  float 浮点数
print("hello %f" % 123.456) # hello 123.456

# 字符串格式化2
# format()
print("hello {}" .format("world")) # hello world

# 字符串格式化3 f-string
a = 123
print(f"hello {a}") # hello 123
name = 'tom'
age = 18
print(f"{name} 今年 {age} 岁") # tom 今年 18 岁

# 动态数据类型
x = 10  # int
x = 10.0 # float
x = "hello" # str
x = True # bool
x = None # NoneType

# 数据类型转换
# int() float() str() bool() None()
print(int(10.9)) # 10
print(int("10")) # 10
# print(int("11hello world")) # ValueError: invalid literal for int() with base 10: '11hello world'

print(float(10)) # 10.0
print(float("10.9")) # 10.9
print(str(10)) # "10"

# 空值为false,有值为true
# 空值：0，'', 空的列表，空字典，空元组，None
print(bool(0)) # False
print(bool(1)) # True  
print(bool(None)) # False
print(bool("")) # False
print(bool("hello")) # True

if "hello": # if True:
    print("有值")  
else: 
    print("空值")

print(bool([])) # False
print(bool([1,2,3])) # True
print(bool({})) # False
print(bool({"a":1})) # True
print(bool(())) # False
print(bool((1,2,3))) # True
print(bool(set())) # False
print(bool(set([1,2,3]))) # True
print(bool(range(10))) # True
print(bool(range(0))) # False
print(bool([1,2,3][0])) # True
# print(bool([1,2,3][3])) # False

# substring切片
# [start:end:step]
# start:起始位置，包含
# end:结束位置，不包含
# step:步长，默认为1
print("hello"[0:3]) # hel
print("hello"[3:])  # lo
print("hello"[0:3:2]) # he
print("hello"[::-1]) # olleh
print("hello"[::-2]) # olh
print("hello"[-2:])

"""
# 函数定义
# 定义一个函数add，接收两个参数a和b
def add(a,b):
    # 返回a和b的和
    return a+b

print(add(1,2)) # 3
print(add("hello"," world")) # hello world

# 函数参数默认值
def add(a,b=10):
    return a+b

print(add(1)) # 11
print(add(1,2)) # 3
"""

# 函数参数类型
# 定义一个函数add，接收两个整数参数a和b，返回它们的和
def add(a:int,b:int=10)->int:
    # 返回a和b的和
    return a+b
print(add(1))    # 3
print(add(1,2)) # 3
print(add("hello"," world"))    # TypeError: unsupported operand type(s) for +: 'int' and 'str'


# 函数可变参数
# 定义一个函数add，接收任意数量的整数参数，返回它们的和
def add(*args):
    # 初始化和为0
    s = 0
    # 遍历参数列表
    for i in args:
        # 将参数累加到和上
        s += i
    # 返回和
    return s

print(add(1,2,3,4,5)) # 15
print(add(1,2,3,4,5,6,7,8,9,10)) # 55

# 可变参数**kwargs
# 定义一个函数add，接收任意数量的关键字参数，返回它们的和
def add(**kwargs):
    # 初始化和为0
    s = 0
    # 遍历关键字参数
    for i in kwargs.values():
        # 将参数累加到和上
        s += i
    # 返回和
    return s

print(add(a=1,b=2,c=3,d=4,e=5)) # 15

# 变量的作用域
# 定义一个全局变量x
x = 10
if 1=1:
    print(x)
    x1 = 20
print(x) # 10
print(x1) # NameError: name 'x1' is not defined
